package com.wgts.wgts.mvp.view.activity.mycenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.mvp.presenter.IShippingAddressActivityPresenter;
import com.wgts.wgts.mvp.view.activity.classification.ClassificationAdapter;
import com.wgts.wgts.netmodle.resp.ShippingAddressEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/2815:35
 *
 */public class AddressRecycleAdapter extends RecyclerView.Adapter<AddressRecycleAdapter.ViewHold> {
    private Context context;
    private List<ShippingAddressEntity> list;
    private onItemClickListen onItemClickListen;
    private FragmentManager manager;
    private IShippingAddressActivityPresenter presenter;

    public AddressRecycleAdapter(Context context, List<ShippingAddressEntity> list, IShippingAddressActivityPresenter presenter,
                                 FragmentManager manager) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
        this.manager = manager;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.address_recycle_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHold viewHold, final int i) {
       if (list.get(i).addressState.equals("1")){
           viewHold.adressCheck.setSelected(true);
           viewHold.isNormalAddress.setText("默认地址");
           viewHold.isNormalAddress.setTextColor(ContextCompat.getColor(context, R.color.black));
       }else {
           viewHold.adressCheck.setSelected(false);
           viewHold.isNormalAddress.setText("设为默认地址");
           viewHold.isNormalAddress.setTextColor(ContextCompat.getColor(context, R.color.hint_6));
       }
       viewHold.name.setText(list.get(i).userName);
       viewHold.phone.setText(list.get(i).userPhone);
       viewHold.adress.setText(list.get(i).province + list.get(i).city +
               list.get(i).area + list.get(i).details);

        viewHold.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListen != null){
                    onItemClickListen.onItemClick(viewHold.itemView, i);
                }
            }
        });

        viewHold.adressCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (i != 0){
                    ToastDate toastDate = new ToastDate();
                    toastDate.setCancelQuestion("是否设置为默认地址？");
                    toastDate.setPointText("确定");
                    toastDate.setCancelText("取消");
                    final ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
                    fragment.show(manager, "ShippingAddressDialog");
                    fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
                        @Override
                        public void onClickCancle() {
                            presenter.normalAddressResult(list.get(i).addressId);
                        }

                        @Override
                        public void onClickEnsuer() {
                            fragment.dismiss();
                        }
                    });
                }
            }
        });

        viewHold.deleteAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastDate toastDate = new ToastDate();
                toastDate.setCancelQuestion("是否删除该地址？");
                toastDate.setPointText("取消");
                toastDate.setCancelText("确定");
                final ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
                fragment.show(manager, "ShippingAddressDialog");
                fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
                    @Override
                    public void onClickCancle() {
                        fragment.dismiss();
                    }

                    @Override
                    public void onClickEnsuer() {
                        presenter.deleteAddress(list.get(i).addressId);
                    }
                });
            }
        });

        viewHold.editorAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AddAddressActivity.class);
                intent.putExtra(AddAddressActivity.ADDRES_INFO, list.get(i));
                ((Activity)context).startActivityForResult(intent, ShippingAddressActivity.AddAddressResult);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.phone)
        TextView phone;
        @BindView(R.id.adress)
        TextView adress;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.adress_check)
        ImageView adressCheck;
        @BindView(R.id.is_normal_address)
        TextView isNormalAddress;
        @BindView(R.id.delete_address)
        TextView deleteAddress;
        @BindView(R.id.editor_address)
        TextView editorAddress;
        private View itemView;
        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public interface onItemClickListen{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListen(onItemClickListen onItemClickListen){
        this.onItemClickListen = onItemClickListen;
    }


}
